from operator import or_

from sqlalchemy import text
from sqlalchemy.sql.operators import or_

from py.PojosAndDaos.Account import Account_class
from py.PojosAndDaos import Record
from py.commonResponse.CommonResponse import *


class AccountService:
    # 展示所有用户
    @classmethod
    def Account_Query_All(cls):
        accountList = Account_class().session.query(Account_class()).filter_by(authority=0).all()
        Account_class().session.close()
        return accountList

    # 展示管理员
    @classmethod
    def Account_Query_All1(cls):
        accountList = Account_class().session.query(Account_class()). \
            filter(or_(Account_class().authority == 1 , Account_class().authority == 2))
        print(accountList)
        Account_class().session.close()
        return accountList

    @classmethod
    def Account_Query_All1(cls):
        accountList = Account_class().session.query(Account_class()).\
            filter(or_(Account_class().authority == 1, Account_class().authority == 2))
        print(accountList)
        Account_class().session.close()
        return accountList
    # 通过ID查询用户
    @classmethod
    def Account_Query_ByReaderID(cls, readerId: int):
        account = Account_class().session.query(Account_class()).filter_by(ReaderID=readerId).one_or_none()
        Account_class().session.close()
        return account

    # 通过ID与密码查询用户
    @classmethod
    def Account_Query_ByPassword(cls, username, password):
        account = Account_class().session.query(Account_class()). \
            filter_by(ReaderName=username, password=password).one_or_none()
        Account_class().session.close()
        return account

    # 通过用户名查询用户
    @classmethod
    def Account_Query_ByUsername(cls, username):
        account = Account_class().session.query(Account_class()).filter_by(ReaderName=username).one_or_none()
        Account_class().session.close()
        return account

    # 用户信息更新
    @classmethod
    def Account_Update(cls, account: Account_class()):
        session = Account_class().session
        if account is None:
            return "没有该用户，无法修改"
        session.query(Account_class()).filter_by(ReaderID=account.ReaderID). \
            update({Account_class().password: account.password,
                    Account_class().Debt: account.Debt,
                    Account_class().isAdmin: account.isAdmin,
                    Account_class().authority: account.authority,
                    Account_class().PhoneNumber: account.PhoneNumber,
                    Account_class().ReaderName: account.ReaderName,
                    Account_class().job: account.job})
        account = AccountService.Account_Query_ByReaderID(account.ReaderID)
        session.commit()
        session.close()
        return createForSuccessWMD(dict(account), "用户信息更新成功！")

    # 用户注销
    @classmethod
    def Account_Delete(cls, ReaderID: int):
        session = Account_class().session
        account = Account_class().session.query(Account_class()).filter_by(ReaderID=ReaderID).one_or_none()
        if account is None:
            return "该用户不存在"
        session.query(Account_class()).filter_by(ReaderID=ReaderID).delete()
        session.commit()
        session.close()
        return "用户已注销！"

    @classmethod
    def Account_Delete(cls, ID: int):
        session = Account_class().session
        session.query(Account_class()).filter_by(ReaderID=ID).delete()
        session.commit()
        session.close()
        return "用户已注销！"

    # 添加用户
    @classmethod
    def Account_Insert(cls, account: Account_class()):
        session = Account_class().session
        account1 = session.query(Account_class()).filter_by(ReaderName=account.ReaderName).one_or_none()
        if account1 is not None:
            session.close()
            return "用户名已被占用！"
        session.execute(
            text(
                "INSERT INTO readertable (ReaderName,password,Debt,PhoneNumber,isAdmin,authority,job) VALUES ('{}','{}','{}','{}','{}','{}','{}');".
                format(account.ReaderName, account.password, account.Debt, account.PhoneNumber,
                       account.isAdmin, account.authority, account.job)))
        session.commit()
        session.close()
        return "用户注册成功！"


if __name__ == '__main__':
    List = AccountService.Account_Query_All1()
    for account in List:
        print(account.__dict__)
